package com.spark.data

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.data.model.NotifyHistoryListModel
import kotlinx.android.synthetic.main.frag_notify_history.*

/**
 * the history about data actions of u
 */
class NotifyHistoryFragment : BaseFragment() {
    private lateinit var notifyHistoryAdapter: NotifyHistoryAdapter
    private var currPage = 1
    private var totalPage = 0
    private var notifyHistoryFilterFragment: NotifyHistoryFilterFragment? = null

    companion object {
        fun newInstance(): NotifyHistoryFragment {
            val fragment = NotifyHistoryFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_notify_history
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        notifyHistoryAdapter = NotifyHistoryAdapter(R.layout.item_notify_history)
        recy_history.adapter = notifyHistoryAdapter
        recy_history.layoutManager = LinearLayoutManager(context)
        iv_category.setOnClickListener {
            notifyHistoryFilterFragment = NotifyHistoryFilterFragment.newInstance()
            notifyHistoryFilterFragment?.show(childFragmentManager, "historyfilter")
            notifyHistoryFilterFragment?.addSearchActionListener {
                fetchNotfiyHistorys(
                        startTime = it.startTime,
                        endTime = it.endTime,
                        messageType = ""
                )
            }
        }

        notifyHistoryAdapter.setOnItemClickListener { adapter, view, position ->
            val data = notifyHistoryAdapter.data[position]
            when (data.taskType) {
            // 借阅申请
                "3" -> {
                    start(BorrowApplyResultFragment.newInstance("${data.id}", "3"))
                }
            // 文件传输
                "4" -> {
                    start(TransferHistoryFragment.newInstance("${data.id}", "4"))
                }
            // 归档申请
                "5" -> {
                }
            }
        }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        fetchNotfiyHistorys(pageNum = "$currPage")
    }

    override fun onBackPressedSupport(): Boolean {
        if (notifyHistoryFilterFragment?.isVisible == true) {
            notifyHistoryFilterFragment?.dismiss()
            return true
        }
        return super.onBackPressedSupport()
    }

    /**
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param pageNum 当前页面
     * @param pageSize
     * @param messageType 任务类型 3 -> 借阅 4 -> 传输
     * @param isRead 任务状态 0 -> 未读 1 -> 已读
     */
    @SuppressLint("CheckResult")
    private fun fetchNotfiyHistorys(startTime: String = "", endTime: String = "", pageNum: String = "1", pageSize: String = "10", messageType: String = "", isRead: String = "1") {
        val params = mutableMapOf(
                Const.qr_startTime to startTime,
                Const.qr_endTime to endTime,
                Const.qr_pageNum to pageNum,
                Const.qr_pageSize to pageSize,
                Const.qr_messageType to messageType,
                Const.qr_isRead to isRead
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_notify_history}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, NotifyHistoryListModel::class.java)?.apply {
                                if (code == "200") {
                                    notifyHistoryAdapter.addData(resultMaps)
                                } else {
                                    error(msg)
                                }
                            }
                        },
                        {
                            it.printStackTrace()
                            report(it)
                            hideLoader()
                        },
                        {
                            hideLoader()
                        },
                        {
                            dispose = it
                            loading()
                        }
                )
    }
}

class NotifyHistoryAdapter(layoutRes: Int) : BaseQuickAdapter<NotifyHistoryListModel.ResultMap, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: NotifyHistoryListModel.ResultMap?) {
        if (item == null) return
        helper?.apply {
            //setText(R.id.tv_title, item.title)
            //setText(R.id.tv_state, item.state)
            //setText(R.id.tv_content, item.description)
        }
    }

}